<template>
    <div>
        <Header :title="isEdit ? '编辑教育经历' : '添加教育经历'"></Header>
        <div class="topheader page">
            <div class="itembox disFlex" v-for="(item, index) in itemList" :key="index" @click="topage(item)">
                <div class="leftItem">
                    <label for="" class="fontBlack73 font28 textL">{{ item.title }}</label>
                    <label for="" class=" font32 fontGrayB9 textL" v-if="!item.data">{{ item.placeholder }}</label>
                    <label for="" class="fontBlack2 font32 textL" v-else>{{ item.data }}</label>
                </div>
                <img :src="require('@/assets/images/chevron-right.png')" class="rightIcon1">

            </div>
            <div class="blockbox"></div>
            <div class="bottom_submitBtn" v-if="!isEdit">
                <div @click="submit" class="font36 bgBlueFC fontWhite">保存</div>
            </div>
            <div class="bottom_submitBtn_two disFlex" v-else>
                <div @click="del" class="font36 bgGrayec fontBlack73">删除</div>
                <div @click="submit" class="font36 bgBlueFC fontWhite">保存</div>
            </div>
        </div>

        <picker-popup :columns="column" :show="show" @change="isConfirm" :type="page"></picker-popup>
        <dialog-btn :title="'是否确认删除'" :show="delshow"></dialog-btn>
        <van-popup :show="maxshow" round position="bottom">
            <van-picker @change="maxChange" :columns="maxList" @cancel="maxshow = false" @confirm="maxsave" />
        </van-popup>
        <dialog-btn :title="'是否保存当前页面'" :show="exitshow"></dialog-btn>

    </div>
</template>

<script>
import Header from '@/components/header.vue';
import PickerPopup from '@/components/pickerpopup.vue';
import DialogBtn from '@/components/dialogbtn.vue';
import { getCurrentInstance, reactive } from 'vue'
import { Toast } from 'vant';
export default {
    components: {
        Header,
        PickerPopup,
        DialogBtn
    },
    data() {
        return {
            itemList: [
                { title: '学校名称', data: '', placeholder: '请输入', page: 'graduateSchool' },
                { title: '学历', data: '', placeholder: '请选择', page: 'education' },
                { title: '专业', data: '', placeholder: '请输入', page: 'profess' },
                { title: '时间段', data: '', placeholder: '请选择', page: 'graduateTime' },
            ],
            isEdit: false,
            show: false,
            page: '',
            column: [],
            delshow: false,
            customFieldName: {},
            maxshow: false, //学历选择框
            maxList: [], //学历选择列表
            token: this.$store.state.token,
            data: {}, //需要回调的参数id和vesion
            paramid: this.$route.params.id,//编辑参数
            obj: {},
            educationInfo: {},
            exitshow: false,
            idcard: this.$store.state.idCard
        }
    },
    mounted() {
        this.getinitInfo();
    },
    methods: {
        // 是否存在信息
        getinitInfo() {
            this.api = getCurrentInstance().appContext.config.globalProperties.$http;
            var that = this;
            var educationInfo = this.$store.state.educationInfo;
            var paramid = this.$store.state.id; //是否为编辑
            that.api.get('/personnel/h5/resumeInfo/queryOne', { identityNumber: that.idcard }).then(res => {
                var data = res.data;

                if (educationInfo.graduateSchool || educationInfo.profess || educationInfo.schoolTime || educationInfo.graduateTime || educationInfo.recruitment) {
                    that.educationInfo = educationInfo;
                    if (paramid) {
                        that.isEdit = true;
                    }
                } else {
                    if (data.resumeEducationRst) {
                        var data1 = data.resumeEducationRst;
                        if (data1.id && data1.version + '') {
                            var myget = {
                                id: data1.id,
                                version: data1.version,
                                identityNumber: data1.identityNumber
                            };
                            that.$store.commit('setmyget', myget);

                        }
                        if (that.paramid) {
                            for (let i = 0; i < data1.educationInfoList.length; i++) {
                                if (data1.educationInfoList[i].id == that.paramid) {
                                    that.$store.commit('seteducationInfo', data1.educationInfoList[i]);
                                    that.$store.commit('setid', that.paramid);
                                    that.educationInfo = data1.educationInfoList[i]
                                    that.isEdit = true;
                                }
                            }


                        }
                        if (data1.educationInfoList.length > 0) {
                            that.$store.commit('seteducationList', data1.educationInfoList);
                        }

                    }
                }


                that.getMyinfo(that.educationInfo);
            });
        },
        getMyinfo(data) {
            var that = this;
            that.itemList[0].data = data.graduateSchool;
            that.itemList[2].data = data.profess;
            if (data.schoolTime && data.graduateTime) {
                that.itemList[3].data = data.schoolTime.slice(0, 4) + '-' + data.graduateTime.slice(0, 4);

            }
            that.geteducation();// 获取学历列表
        },
        topage(e) {
            if (e.placeholder == '请输入') {
                this.$router.push({ name: 'school', params: { page: e.title } });
            } else if (e.placeholder == '请选择') {
                this.page = e.page;
                if (e.page == 'graduateTime') {
                    let currentYear = new Date().getFullYear();
                    let years = [];
                    let n = 50;
                    for (let i = 0; i < n; i++) {
                        currentYear--;
                        years.push(currentYear);
                    }
                    years = years.reverse()
                    this.column = [{
                        values: years,
                        defaultIndex: n - 1
                    }, {
                        values: years.concat(new Date().getFullYear()),
                        defaultIndex: n
                    }];
                    this.show = true;
                } else if (e.page == 'education') {
                    this.maxshow = true;
                }

            }

        },
        isClose() {
            this.show = false;
        },
        isConfirm(value, index, type) {
            for (let i of this.itemList) {
                if (i.page == type) {
                    if (value instanceof Array) {
                        if (value[1] < value[0]) {
                            Toast('时间选择错误')
                            return
                        }
                        i.data = value[0] + '-' + value[1];
                    } else {
                        i.data = value;
                    }
                }
            }
            this.educationInfo.schoolTime = value[0] + '-09-01'
            this.educationInfo.graduateTime = value[1] + '-07-01',
                this.$store.commit('seteducationInfo', this.educationInfo);
            this.show = false;
        },
        submit() {
            for (let i of this.itemList) {
                if (!i.data) {
                    Toast(i.placeholder + i.title);
                    return;
                }
            }
            var data = this.$store.state.myget;
            data.identityNumber = this.token;
            var list = this.$store.state.educationList;
            if (this.$store.state.id) {
                for (let i = 0; i < list.length; i++) {
                    if (this.$store.state.id == list[i].id) {
                        list[i] = this.educationInfo;
                        list[i].resumeId = data.id;
                        list[i].graduateTime = list[i].graduateTime.length == 10 ? list[i].graduateTime : list[i].graduateTime.split('-')[1] + '-07-01';
                        list[i].schoolTime = list[i].schoolTime.split('-')[0] + '-09-01';
                    }
                }
            } else {
                var obj = Object.assign({}, this.educationInfo);
                obj.resumeId = data.id;
                // obj.graduateTime = obj.graduateTime.split('-')[1] + '-07-01';
                obj.schoolTime = obj.schoolTime.split('-')[0] + '-09-01';
                list.push(obj);
            }
            data.educationInfoList = list;
            var that = this;
            that.api.post('/personnel/h5/resumeInfo/addOrEditEducation', data).then(res => {
                if (res.code == 200) {
                    that.$store.commit('seteducationInfo', {});
                    that.$store.commit('setid', '');
                    that.$store.commit('setmyget', {});
                    that.$store.commit('seteducationList', []);
                    that.$router.replace('/resume')
                }
            })
        },
        del() {
            this.delshow = true;
        },
        ispopSub() {
            if (this.delshow) {
                let that = this;
                this.delshow = false;
                this.itemList[0].data = '';
                this.itemList[1].data = '';
                this.itemList[2].data = '';
                this.itemList[3].data = ''
                this.api.get('/personnel/h5/resumeInfo/deleteEducation', { id: this.$store.state.id }).then(res => {
                    if (res.code == 200) {
                        that.$store.commit('seteducationInfo', {});
                        that.$store.commit('setid', '');
                        that.$store.commit('setmyget', {});
                        that.$store.commit('seteducationList', []);
                        that.$router.replace('/resume')
                    }
                })
                return
            }
            if (this.exitshow) {
                this.exitshow = false;
                this.submit();
                return;
            }

        },
        // 获取学历列表
        geteducation() {
            var that = this;
            that.maxList = [];
            that.api.get('/personnel/h5/resumeInfo/resumeEducation', {}).then(res => {
                var data = res.data.resumeEducation;
                var arr = [{
                    text: '统招',
                    values: 1
                }, {
                    text: '非统招',
                    values: 0
                }];
                var recruitment = this.$store.state.educationInfo.recruitment;
                for (let i = 0; i < data.length; i++) {
                    var obj = {};
                    obj.text = data[i].dictLabel;
                    obj.values = data[i].dictValue;
                    obj.children = [];
                    if (obj.text != '文盲' && obj.text != '小学' && obj.text != '初中' && obj.text != '初中及以下' && obj.text != '高中' && obj.text != '职高' && obj.text != '中专高中') {
                        obj.children = arr;
                    }
                    if (this.$store.state.educationInfo.education == obj.values) {
                        that.itemList[1].data = obj.text + (recruitment == 1 ? '·统招' : recruitment == 0 ? '·非统招' : '');
                        that.itemList[1].val = obj.values;
                        that.itemList[1].re = recruitment;
                    }
                    that.maxList.push(obj);
                };
            })
        },
        // 弹窗确认保存
        maxsave(value) {
            this.educationInfo.education = value[0].values;
            this.itemList.filter((item, i) => {
                if (item.page == 'education') {
                    if (value[1]) {
                        this.itemList[i].data = value[0].text + (value[1].values == 0 ? '·非统招' : value[1].values == 1 ? '·统招' : '');
                    } else {
                        this.itemList[i].data = value[0].text;
                    }

                }
            })
            if (value[1]) {
                this.educationInfo.recruitment = value[1].values;
            }
            this.$store.commit('seteducationInfo', this.educationInfo);
            this.maxshow = false;
        },
        ispopClose() {
            this.delshow = false;
            if (this.exitshow) {
                this.exitshow = false;
                this.$store.commit('seteducationInfo', {});
                this.$store.commit('setid', '');
                this.$store.commit('setmyget', {});
                this.$store.commit('seteducationList', []);
                this.$router.replace('/resume')
            }
        },
        goback() {
            if (JSON.stringify(this.educationInfo) != '{}') {
                this.exitshow = true;
            } else {
                this.$store.commit('seteducationInfo', {});
                this.$store.commit('setid', '');
                this.$store.commit('setmyget', {});
                this.$store.commit('seteducationList', []);
                this.$router.replace('/resume')
            }
        }

    }
}
</script>

<style scoped>
.itembox {
    border-bottom: 0.02rem solid #E8E9EC;
    height: 1.4rem;
}

.leftItem {
    display: flex;
    flex-direction: column;
}

.leftItem label:nth-child(1) {
    margin-bottom: 0.08rem;
}

.blockbox {
    height: 1.6rem;
}
</style>